package com.hippo.javacore.lambda;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @ClassName LambdaOptional
 * @Description TODO Lambda表达式操作
 * @Author tangxl
 * @create 2023-05-31 09:58
 **/
public class LambdaOptional {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("007","002","003","004","0015","0011"));
        // 遍历
        list.forEach(System.out::println);
        list.forEach(o-> System.out.println("遍历："+o));


        // 排序
        // 正常
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // 正序
                return o1.compareTo(o2);
                // 逆序
//                return o2.compareTo(o1);
            }
        });
        System.out.println("正常排序操作："+list.toString());
        // 正序
        Collections.sort(list, ((o1, o2) -> o1.compareTo(o2)));
        System.out.println("Lambda排序-正序操作"+list.toString());
        // 逆序
        Collections.sort(list, (o1,o2)-> o2.compareTo(o1));
        System.out.println("Lambda排序-逆序操作："+list.toString());


        // 过滤：过滤掉数据 '003'
        // 常规过滤
        List<String> list2 = new ArrayList<>();
        list.forEach(o->{
            if(!o.equals("003")){
                list2.add(o);
            }
        });
        System.out.println("常规过滤后："+list2.toString());
        // Lsmbda操作过滤
        List<String> list3 = new ArrayList<>();
        list3 = list.stream().filter(o -> !o.equals("003")).collect(Collectors.toList());
        System.out.println("Lambda操作过滤："+list3);


        // 映射：取出每个元素的长度
        // 常规操作
        List<Integer> list4 = new ArrayList<>();
        list.forEach(o -> {
            list4.add(o.length());
        });
        System.out.println("常规操作映射："+list4);
        // Lambda操作
        List<Integer> list5 = list.stream().map(o->o.length()).collect(Collectors.toList());
        System.out.println("Lambda操作映射："+list5);


        // 归约：求集合中所有数据的和
        List<Integer> list6 = new ArrayList<>(Arrays.asList(3,5,1,9,7));
        // 常规操作
        Integer sum1 = 0;
        for (Integer i:list6) {
            sum1 += i;
        }
        System.out.println("常规操作归约："+sum1);
        // Lambda操作
        int sum2 = list6.stream().reduce(0,(a, b) -> a + b); // 0为起始值
        System.out.println("Lambda操作归约："+sum2);


        // 分组：
        // 常规操作分组
        Map<Integer,List<String>> group1 = new HashMap<>();
        for (String o: list) {
            int length = o.length();
            if(!group1.containsKey(length)){
                group1.put(length,new ArrayList<>(Arrays.asList(o)));
            }else {
                List<String> strings = group1.get(length);
                strings.add(o);
                group1.put(length, strings);
            }
        }
        System.out.println("常规操作分组："+group1);
        // Lambda操作分组
        Map<Integer,List<String>> group2 = list.stream().collect(Collectors.groupingBy(String::length));
        System.out.println("Lambda操作分组："+group2);


        // 函数式接口的实现
        // 常规操作函数式接口的实现
        MyInterface myInterface1 = new MyInterface() {
            @Override
            public void doSomething(String s) {
                System.out.println(s);
            }
        };
        myInterface1.doSomething("常规操作函数式接口的实现");
        // Lambda操作函数式接口的实现
        MyInterface myInterface2 = (s) -> System.out.println(s);
        myInterface2.doSomething("Lambda操作函数式接口的实现");


        // 线程的创建
        // 常规操作创建线程
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("常规操作创建线程");
            }
        });
        thread1.start();
        // Lambda操作创建线程
        Thread thread2 = new Thread(()-> System.out.println("Lambda操作创建线程"));
        thread2.start();


        // Optional操作:如果字符串不为空就转为大写
        // 常规操作实现Optional操作
        String str1 = "hello";
        if (str1!=null) {
            str1.toUpperCase();
        }
        System.out.println("常规操作实现Optional操作:"+str1);
        // Lambda实现Optional操作
        String str2 = "hello";
        Optional.ofNullable(str2).map(String::toUpperCase).ifPresent(System.out::println);

        // Stream的流水线操作:取出元素每个元素首字母为 'a' 的转为大写
        List<String> list7 = new ArrayList<>(Arrays.asList("apple","dog","alimo"));
        List<String> list8 = new ArrayList<>();
        // 常规操作
        for (String str:list7) {
            if(str.startsWith("a")){
                list8.add(str.toUpperCase());
            }
        }
        System.out.println("常规操作："+list8);
        // Lambda操作
        List<String> list9 = list7.stream().filter(o->o.startsWith("a")).map(o->o.toUpperCase()).collect(Collectors.toList());
        System.out.println("Lambda操作："+list9);
    }
}

interface MyInterface {
    void doSomething(String s);
}
